package com.kadron.jobs;

import java.util.Date;
import java.util.List;
import java.util.TimerTask;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import com.kadron.common.KUtil;
import com.kadron.dao.KUserDAO;
import com.kadron.entity.User;
import com.kadron.mail.KMailService;

@AutoCreate
@Name(KUserBirthdayJob.K_NAME)
@Scope(ScopeType.EVENT)
public class KUserBirthdayJob extends TimerTask {
	
	public static final String K_NAME = "KUserBirthdayJob";
	protected static final String INFO_MESASGE = "RUN job"+K_NAME;
	
	private static final Log log = LogFactory.getLog(KUserBirthdayJob.class);

	@In
	private KUserDAO userDAO;
	
	@Override
	public void run() {
		if (log.isInfoEnabled()) {
			log.info(INFO_MESASGE);
		}
		Date moment = KUtil.getMoment();
		try {
			List<User> users = userDAO.getBirthdayNearUsers(moment);
			if (users.isEmpty()) {
				if (log.isInfoEnabled()) {					
					log.info("--- NO USERS FOR BIRTHDAY REMAINDER ---");
				}
				return;
			}
			List<User> admins = userDAO.getAdminUsers();
			for (User admin : admins) {				
				KMailService.sendBirtdayRemainder(users, admin);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	
}
